﻿#include "EditorPackagePageView.h"

#include "Widgets/Positioners/VerticalPositionerWidget.h"
#include "Widgets/TextWidget.h"
#include "Widgets/Mouse/MouseWidget.h"
#include "Widgets/ImageWidget.h"
#include "Widgets/RectangleWidget.h"
#include "Widgets/EditorEditableTextWidget.h"
#include "Widgets/ButtonWidget.h"
#include "Widgets/EditorImageButton.h"
#include "Widgets/EditorTextButton.h"
#include "Widgets/Windows/FileDialog.h"
#include "Widgets/EditableTextWidget.h"

#include "Asset/EditorDefaultPackage.h"


void REditorPackagePageView::BindViewModel(TSharedObjectPtr<REditorPageViewModel> InViewModel)
{
    ViewModel =InViewModel;
}

void REditorPackagePageView::CreateWidget(TSharedObjectPtr<RWidget> InParent)
{
    auto Root= NewWidget<RVerticalPositionerWidget>(InParent);
    Root->Anchor.CenterIn(InParent);
    Root->Spacing=10;

    const float WindowWidth=300;
    Root->Width=WindowWidth;
    const float TextLineHeight=30;
    Root->Height=TextLineHeight*4;


    auto OutputOption= NewWidget<RVerticalPositionerWidget>(Root);
    OutputOption->Height=TextLineHeight*2;
    OutputOption->Anchor.HorizontalFill(Root);
    OutputOption->Spacing=5;

    auto OutputOptionText= NewWidget<RTextWidget>(OutputOption);
    OutputOptionText->Text=U"输出文件夹：";
    OutputOptionText->Height=TextLineHeight;
    OutputOptionText->Anchor.HorizontalFill(OutputOption);
    
    auto OutputOptionPath= NewWidget<RWidget>(OutputOption);
    OutputOptionPath->Height=TextLineHeight;
    OutputOptionPath->Anchor.HorizontalFill(OutputOptionText);

    auto OutputOptionPathText=WHE::CreateEditableTextWidget(OutputOptionPath);
    OutputOptionPathText.Widget->Width=WindowWidth-50;
    OutputOptionPathText.Widget->Anchor.VerticalFill(OutputOptionPath);
    OutputOptionPathText.TextWidget->Text=[this](){
        return ViewModel->GetOutputPath();
    };
    OutputOptionPathText.TextWidget->UpdateTextOnAccepted=false;
    OutputOptionPathText.TextWidget->UpdateTextOnLostFocus=false;
    OutputOptionPathText.TextWidget->OnTextUpdated.BindLambda([this](const CString& NewText)
    {
        ViewModel->SetOutputPath(NewText);
    });


    WHE::SImageButtonCreateInfo BrowseButtonInfo;
    BrowseButtonInfo.ToolTip=U"浏览文件夹";
    BrowseButtonInfo.ParentWidget=OutputOptionPath;
    BrowseButtonInfo.Icon=REditorDefaultPackage::Get()->GetIcon(U"CodiconEllipsis");
    BrowseButtonInfo.OnClick=[this]()
    {
        auto ProjectPath=WH::CreateFolderDialog(U"选择打包输出目录");
        if(ProjectPath.HasValue())
        {
            ViewModel->SetOutputPath(ProjectPath.GetValue().ToString());
        }
    };




    auto OutputOptionPathButton= WHE::CreateImageButton(BrowseButtonInfo);
    OutputOptionPathButton->Anchor.LeftReference=OutputOptionPathText.Widget;
    OutputOptionPathButton->Anchor.LeftReferenceEdge=EHorizontalWidgetEdge::Right;
    OutputOptionPathButton->Anchor.LeftOffset=5;
    OutputOptionPathButton->Anchor.VerticalFill(OutputOptionPath);

    WHE::SImageButtonCreateInfo ClearFolderButtonInfo;
    ClearFolderButtonInfo.ToolTip=U"清空所选文件夹";
    ClearFolderButtonInfo.ParentWidget=OutputOptionPath;
    ClearFolderButtonInfo.Icon=REditorDefaultPackage::Get()->GetIcon(U"CodiconRefresh");
    ClearFolderButtonInfo.OnClick=[this]()
    {
        ViewModel->DeleteItemInOutputFolder();
    };

    auto ClearFolderButton= WHE::CreateImageButton(ClearFolderButtonInfo);
    ClearFolderButton->Anchor.LeftReference=OutputOptionPathButton;
    ClearFolderButton->Anchor.LeftReferenceEdge=EHorizontalWidgetEdge::Right;
    ClearFolderButton->Anchor.LeftOffset=5;
    ClearFolderButton->Anchor.VerticalFill(OutputOptionPath);


    auto PlatformOption= NewWidget<RVerticalPositionerWidget>(Root);
    PlatformOption->Height=TextLineHeight*2;
    PlatformOption->Anchor.HorizontalFill(Root);
    PlatformOption->Spacing=5;

    //目前并不支持跨平台编译 所以这里只是显示
    auto PlatformOptionLabel= NewWidget<RTextWidget>(PlatformOption);
    PlatformOptionLabel->Text=U"目标平台："+ViewModel->GetPlatform();
    PlatformOptionLabel->Height=TextLineHeight;
    PlatformOptionLabel->Anchor.HorizontalFill(PlatformOption);


    auto PackageButton=WHE::CreateTextButton(
        U"打包", 
        [this](){
            ViewModel->Package();
        }
        , Root);
    PackageButton->Height=TextLineHeight;
    PackageButton->Width=100;
    PackageButton->Anchor.TopReference=PlatformOption;
    PackageButton->Anchor.TopReferenceEdge=EVerticalWidgetEdge::Bottom;
    PackageButton->Anchor.TopOffset=10;
    PackageButton->Anchor.RightReference=PlatformOption;
    PackageButton->Anchor.RightReferenceEdge=EHorizontalWidgetEdge::Right;
    PackageButton->Anchor.RightOffset=0;
}

